package com.lishe.lishe.lishe.module.excitation.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lishe.lishe.R;
import com.lishe.lishe.autoScrollListView.AutoScrollListView;
import com.lishe.lishe.lishe.module.excitation.bean.HeadInfo;

import java.util.List;

/**
 * Created by shino on 2017/12/11.
 */

public class HeadScrollViewAdapter extends BaseAdapter implements AutoScrollListView.AutoScroll {
    private Context context;
    private List<HeadInfo> infos;

    public HeadScrollViewAdapter(Context context, List<HeadInfo> infos) {
        this.context = context;
        this.infos = infos;
    }

    @Override
    public int getCount() {
        return infos == null ? 0 : infos.size();
    }

    @Override
    public Object getItem(int i) {
        return infos.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = View.inflate(context, R.layout.item_excitation_head_info, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        final HeadInfo headInfo = infos.get(position);
        holder.title.setText(headInfo.getFlag());
        holder.content.setText(headInfo.getContent());
        if (!TextUtils.isEmpty(headInfo.getEntryDate())) {
            int index = headInfo.getEntryDate().indexOf("-");
            String time = headInfo.getEntryDate().substring(index + 1);
            time = time.replace("-", "月") + "日";
            holder.time.setText(time);
        }
        return view;
    }

    @Override
    public int getListItemHeight(Context context) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, context.getResources().getDisplayMetrics());
    }

    @Override
    public int getImmovableCount() {
        return scrollCount;
    }

    class ViewHolder {
        TextView title;
        TextView time;
        TextView content;

        public ViewHolder(View itemView) {
            time = itemView.findViewById(R.id.time);
            title = itemView.findViewById(R.id.title);
            content = itemView.findViewById(R.id.content);
        }
    }
    int scrollCount=3;
    public void setScrollCount(int count){
        scrollCount=count;
        notifyDataSetChanged();
    }
}
